<?php
	
	class st_registry {
		const REGISTRY_SERIALIZABLE = true;
		const REGISTRY_NORMAL = false;
		protected static $_instance = null;
		protected $_registry = array();
		protected $_config   = array(
								'encode'   => false,
								'expired'  => 30,
								'limiter'  => 'private',
								'savePath' => 'default',
				             );
		
		function __construct() {
			$this->_settings();
		}
		
		protected function _settings() {
			if (session_id() === '') {
				session_start();
			}
			if (count($this->_config) === 0) {
				return $this;	
			}
			if (isset($this->_config['expired'])) {
				session_cache_expire($this->_config['expired']);	
			}
			if (isset($this->_config['limiter'])) {
				session_cache_limiter($this->_config['limiter']);	
			}
			if (isset($this->_config['savePath'])) {
				if ($this->_config['savePath'] != 'default') {
					session_save_path($this->_config['savePath']);
				}
			}
			return $this;
		}
		
		public static function getInstance() {
			if (null === self::$_instance) {
				self::$_instance = new self;	
			}
			return self::$_instance;
		}
		
		public function register($key, $value, $serialize = false) {
			if (empty($key)) {
				return false;	
			}
			if ($serialize) {
				$value = serialize($value);	
			}
			$this->_registry[$key]  = $value;
			$_SESSION[$key]['data'] = $value;
			$_SESSION[$key]['serialize'] = (int)$serialize;
			return $this;	
		}
		
		public function registry($key) {
			if (!is_string($key)) {
				throw new Exception("Key Of Registry Object Must Be A String");
			}
			$value = null;
			if (isset($_SESSION[$key]['data'])) {
				if ((int)$_SESSION[$key]['serialize'] === 1) {
					$value = unserialize($_SESSION[$key]['data']);							   
				} else {
					$value = (string)$_SESSION[$key]['data'];	
				}
			}
			return $value;
		}
		
		public function clearRegistry($key = null)
		{
			if (!$key) {
				$_SESSION = array();
				return $this;
			}
			unset($_SESSION[$key]);
			return $this;
		}
		
		public function getFlashMessage($key)
		{
			$message = $this->registry($key);
			unset($_SESSION[$key]);
			return $message;
		}
	}
?>